#include<iostream>
using namespace std;
#include<vector>
#include<utility>
#include<algorithm>
#include<map>
map<int, int> r;
typedef struct goods
{
    int num;
    map<int, int> tips;
    int value;
}goods;
int cmp(goods& a, goods& b)
{
    int cost1 = 0;
    int cost2 = 0;
    int i = 0;
    map<int, int>::iterator it = a.tips.begin();
    while (it != a.tips.end())
    {
        cost1 += r[it->first]*(it->second);
        it++;
    }
    map<int, int>::iterator it2 = b.tips.begin();
    while (it2 != b.tips.end())
    {
        cost2 += r[it2->first]*(it2->second);
        it2++;
    }
    cost1 -= a.value;
    cost2 -= b.value;
    if (cost2 >= cost1)
        return 0;
    return 1;
}
int cmp2(goods& a, goods& b)
{
    if (b.value - a.value < 0)
        return 1;
    return 0;
}
int main()
{

    int n;
    cin >> n;
    int arr[1002][3] = { 0 };
    int arr2[1002][3] = { 0 };
    vector<goods> cater;
    cater.resize(n);
    int i = 0;
    for (i = 0; i < n; i++)
    {
        int a, b, c;
        cin >> a >> b >> c;
        arr[i][0] = a;
        arr[i][1] = b;
        arr[i][2] = c;
        arr2[i][0] = a;
        arr2[i][1] = b;
        arr2[i][2] = c;
        cater[i].num = 1;
        cater[i].tips.insert(pair<int, int>(a, 1));
        cater[i].value = c;
        r.insert(pair<int, int>(a, c));
    }
    
    int k;
    cin >> k;

    cater.resize(k + n);
    for (i = n; i < k + n; i++)
    {
        cin >> cater[i].num;
        int j = 0;
        for (j = 0; j < cater[i].num; j++)
        {
            int a, b;
            cin >> a >> b;
            cater[i].tips.insert(pair<int, int>(a, b));
        }   
        cin >> cater[i].value;
    }
    vector<goods> cater2=cater;
    sort(cater.begin(), cater.end(), cmp);
    sort(cater2.begin(), cater2.end(), cmp2);
    int sum = 0;
    int all = n;
    while (n>0)
    {
        int i = 0;
        for (i = 0; i < cater.size(); i++)
        {
            if (cater[i].num <= n)
            {
                int flag = 1;
                while (flag)
                {
                    int j = 0;
                    int ff = 0;
                    for (j = 0; j < all; j++)
                    {
                        if (cater[i].tips.find(arr[j][0]) != cater[i].tips.end())
                        {
                            ff = 1;
                            if (arr[j][1] < cater[i].tips[arr[j][0]])
                            {
                                flag = 0;
                                break;
                            }
                        }
                    }
                    if (ff == 0)
                        break;
                    if (flag)
                    {
                        for (j = 0; j < all; j++)
                        {
                            if (cater[i].tips.find(arr[j][0]) != cater[i].tips.end())
                            {
                                arr[j][1] -= cater[i].tips[arr[j][0]];
                                if (arr[j][1] == 0)
                                    n--;
                            }
                        }
                        sum += cater[i].value;
                    }
                }
            }
        }

    }
    int sum2 = 0;
    n = all;
    while (n > 0)
    {
        int i = 0;
        for (i = 0; i < cater2.size(); i++)
        {
            if (cater2[i].num <= n)
            {
                int flag = 1;
                while (flag)
                {
                    int j = 0;
                    int ff = 0;
                    for (j = 0; j < all; j++)
                    {
                        if (cater2[i].tips.find(arr2[j][0]) != cater2[i].tips.end())
                        {
                            ff = 1;
                            if (arr2[j][1] < cater2[i].tips[arr2[j][0]])
                            {
                                flag = 0;
                                break;
                            }
                        }
                    }
                    if (ff == 0)
                        break;
                    if (flag)
                    {
                        for (j = 0; j < all; j++)
                        {
                            if (cater2[i].tips.find(arr2[j][0]) != cater2[i].tips.end())
                            {
                                arr2[j][1] -= cater2[i].tips[arr2[j][0]];
                                if (arr2[j][1] == 0)
                                    n--;
                            }
                        }
                        sum2 += cater2[i].value;
                    }
                }
            }
        }

    }
    cout << min(sum,sum2);
}
